﻿#region Header
// ------------------------ Licence / Copyright ------------------------
// 
// ScrumTable for Scrum Meeting Support
// Copyright © HSR - Hochschule für Technik Rapperswil
// All Rights Reserved
// 
// Author:
//  Michael Gfeller Silvan Gehrig Patrick Boos
// 
// ---------------------------------------------------------------------
#endregion

#region Usings

using System;
using System.Collections.Generic;
using System.IO;
using System.Windows;
using ScrumTable.BL.DM.DataManagement;
using ScrumTable.Common.FileSystem;
using ScrumTable.Common.Unity;
using ScrumTable.UI.ViewModel.Data;

#endregion

namespace ScrumTable.UI.ViewModel.Common
{
    /// <summary>
    /// Loads custom gui components
    /// </summary>
    public static class GuiLoader
    {

        #region Declarations
        //--------------------------------------------------------------------
        // Declarations
        //--------------------------------------------------------------------
        private static Dictionary<string, Dictionary<string, string>> _customGuis = new Dictionary<string, Dictionary<string, string>>();
        #endregion

        #region Properties
        //--------------------------------------------------------------------
        // Properties
        //--------------------------------------------------------------------
        
        
       


        #endregion

        #region Constructors / Destructor
        //--------------------------------------------------------------------
        // Constructors / Destructor
        //--------------------------------------------------------------------
        static GuiLoader()
        {
            Init();
        }

      
        #endregion

      
        #region Methods
        //--------------------------------------------------------------------
        // Methods
        //--------------------------------------------------------------------

        /// <summary>
        /// Gets the string for a gui custom component
        /// </summary>
        /// <param name="componentName">file name stored under the gui config folder (current project)</param>
        /// <returns>the custom gui as string else null</returns>
        public static string GetCustomGuiFor(string componentName)
        {
            componentName = (componentName.Contains(".")) ? componentName : componentName + ".xaml";

            string projectName = ((IAppScreen) Application.Current.MainWindow).Container.Resolve<ScrumViewModelData>().CurrentProject.Name;
            if (_customGuis.ContainsKey(projectName) && _customGuis[projectName].ContainsKey(componentName))
            {
                return _customGuis[projectName][componentName];
            }
            return null;
        }

        private static void Init()
        {
            string directoryRoot = Path.Combine(DataFolderUtil.AssemblyDirectory, @"Config\Gui\");
            if (!Directory.Exists(directoryRoot))
            {
                return;
            }
            string[] directories = Directory.GetDirectories(directoryRoot);
            foreach (var directory in directories)
            {
                Dictionary<string, string> files = new Dictionary<string, string>();
                foreach (string file in Directory.GetFiles(directory))
                {
                    using (StreamReader fs = new StreamReader(file))
                    {
                        files[new FileInfo(file).Name] =  fs.ReadToEnd();
                    }
                }
                _customGuis[new FileInfo(directory).Name] = files;
            }
        }

        #endregion

    }

   
}